import time
import pandas as pd
import cProfile
import multiprocessing
from SentimentCalculator import SentimentCalculator
import tracemalloc
from multiprocesspandas import applyparallel
from pandas_parallel_apply.series_parallel import SeriesParallel
from pandarallel import pandarallel

if __name__ == '__main__':
    df = pd.read_csv('./结果文件/董宇辉/董宇辉.csv')
    start_time = time.time()
    calculator = SentimentCalculator()
    tracemalloc.start()
    pandarallel.initialize(progress_bar=True,nb_workers=multiprocessing.cpu_count()-2)
    try:
        # df['sentiment_score'] = applyparallel.series_apply_parallel(self=df['微博正文'],func=calculator.sentiment_score, num_processes=multiprocessing.cpu_count()-2)
        # df['sentiment_score'] = df['微博正文'].apply_parallel(calculator.sentiment_score, num_processes=multiprocessing.cpu_count()-2)
        # df['sentiment_score'] = SeriesParallel(df['微博正文'],n_cores=multiprocessing.cpu_count()-2).apply(calculator.sentiment_score)
        # df['sentiment_score'] = df['微博正文'].apply(calculator.sentiment_score)
        cProfile.run("df['sentiment_score'] = df['微博正文'].apply(calculator.sentiment_score)")
    except Exception as e:
        print(e)
    current_snapshot = tracemalloc.take_snapshot()
    max_memory = max(stat.size for stat in current_snapshot.statistics('lineno'))
    print(f"Max memory used: {max_memory / 10**6} MB")
    df.to_csv('./结果文件/董宇辉/董宇辉1.csv', index=False)
    end_time = time.time()
    print('耗时:', end_time - start_time)
    print(max(df['sentiment_score']))
    print(min(df['sentiment_score']))
